﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace VisualClockControl
{
    public partial class VisualColck : UserControl
    {
        #region 字段
        static PointF[] ptSed;
        static PointF[] ptMin;
        static PointF[] ptHour;
        #endregion

        #region 方法
        public VisualColck()
        {
            InitializeComponent();

            setPT();

            timerUpdataUI.Interval = 1000;
            timerUpdataUI.Tick += new EventHandler(timerUpdataUI_Tick);

            this.SizeChanged += new EventHandler(VisualColck_SizeChanged);

            //g = picBoxClockView.CreateGraphics();
            timerUpdataUI.Start();
        }

        /// <summary>
        /// 根据控件大小设置时分秒针的大小
        /// </summary>
        private void setPT()
        {
            int scaleValue = (this.Width + this.Height) / 4;
            float y1 = scaleValue * 0.8f;
            float y2 = scaleValue * 0.1f;
            float x1 = scaleValue * 0.02f;

            ptSed = new PointF[] { new PointF(0, y1), new PointF(-x1, 0), new PointF(0, -y2 - 7), new PointF(x1, 0) };
            ptMin = new PointF[] { new PointF(0, y1 - 1.5f * y2), new PointF(-0.5f - x1, 0), new PointF(0, -y2 - 6), new PointF(0.5f + x1, 0) };
            ptHour = new PointF[] { new PointF(0, y1 - 2.3f * y2), new PointF(-1 - x1, 0), new PointF(0, -y2 - 5), new PointF(1 + x1, 0) };
        }

        void VisualColck_SizeChanged(object sender, EventArgs e)
        {
            setPT();
            //throw new NotImplementedException();
        }

        void timerUpdataUI_Tick(object sender, EventArgs e)
        {
            this.Invalidate();
            //throw new NotImplementedException();
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            g.TranslateTransform(this.Width / 2, this.Height / 2);
            g.ScaleTransform(1, -1);
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

            DateTime time = DateTime.Now;

            System.Drawing.Drawing2D.GraphicsState state = g.Save();
            PointF x = new PointF(-8, (this.Height / 2) * 0.8f);
            for (int i = 1; i < 13; i++)
            {
                //g.DrawLine(new Pen(Color.Black, 1), x, y);
                g.RotateTransform(-30);
                g.DrawString(i.ToString(), this.Font, Brushes.Black, x);

            }
            g.Restore(state);

            //绘制时针
            state = g.Save();
            g.RotateTransform(-(float)(30.0 * (time.Hour + time.Minute / 60.0 + time.Second / 3600.0)));
            g.FillPolygon(Brushes.Black, ptHour);
            g.Restore(state);

            //绘制分针
            state = g.Save();
            g.RotateTransform(-(float)(6.0 * (time.Minute + time.Second / 60.0)));
            g.FillPolygon(Brushes.Blue, ptMin);
            g.Restore(state);

            //绘制秒针
            state = g.Save();
            g.RotateTransform(-(float)(6.0 * time.Second));
            g.FillPolygon(Brushes.Red, ptSed);
            //g.DrawString("●", this.Font, Brushes.Black, new PointF(-7, -5));
            g.Restore(state);
        }

        #region 公开控制方法
        public void StartTick()
        {
            if (!timerUpdataUI.Enabled)
                timerUpdataUI.Start();
        }

        public void PauseTick()
        {
            if (timerUpdataUI.Enabled)
                timerUpdataUI.Stop();
        }

        public void ReStartTick()
        {
            if (timerUpdataUI.Enabled)
            {
                timerUpdataUI.Stop();
            }
            timerUpdataUI.Start();
        }
        #endregion

        #endregion
    }
}
